package work.flow.demo.config.db;

import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import work.flow.demo.mapper.branch.BranchMapper;


@Configuration
public class SqlSessionFactoryConfig {
    private static final Logger logger = LoggerFactory.getLogger(SqlSessionFactoryConfig.class);

    @Bean(name = {"MainSqlSessionFactory"})
    public SqlSessionFactory sqlSessionFactory() {
        logger.warn("=======>MyBatisSessionFactory初始化了<=======");
        Environment environment = new Environment("development", new JdbcTransactionFactory(), new CustomDruidDataSource());
        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration(environment);
        //开启驼峰规则
        configuration.setMapUnderscoreToCamelCase(true);
        //测试环境-开启日志
        configuration.setLogImpl(org.apache.ibatis.logging.stdout.StdOutImpl.class);
        //注册mapper
        mapperRegister(configuration);
        return new SqlSessionFactoryBuilder().build(configuration);
    }

    private void mapperRegister(org.apache.ibatis.session.Configuration configuration) {
        configuration.addMapper(BranchMapper.class);
    }


}
